#include <fstream>
#include <sstream>
#include "FileInterface.h"
#include "strmanip.h"

// ----- Begin of Public Methods

pn::FileInterface::FileInterface(const std::string& rInputFilePath)
{
	setInputFilePath(rInputFilePath);
}

pn::FileInterface::~FileInterface()
{
	
}

void pn::FileInterface::setInputFilePath(const std::string& rInputFilePath)
{
	mInputFilePath.assign(rInputFilePath.c_str());
}

bool pn::FileInterface::readInputFile()
{
	std::ifstream inputFile(mInputFilePath.c_str(), std::ios::in);
	if (!inputFile.good()) return false;
	std::string line;
	while (!inputFile.eof())
	{
		inputFile >> line;
		readLine(line);
		line.clear();
	}
	inputFile.close();
	return true;
}

std::map<std::string, pn::Place*> pn::FileInterface::getPlaces()
{
	std::vector<std::string>::iterator placesDataIterator			= mPlaces.begin();
	std::vector<std::string>::iterator initialMarkupDataIterator	= mMarkups.begin();
	std::map<std::string, pn::Place*> placesMap;
	while ((placesDataIterator != mPlaces.end()) && (initialMarkupDataIterator != mMarkups.end()))
	{
		std::string placeId(*placesDataIterator);
		std::istringstream streamInitialMarks(*initialMarkupDataIterator);
		unsigned int placeTokens;
		streamInitialMarks >> std::dec >> placeTokens;
		placesMap[placeId] = new pn::Place(placeId, placeTokens);
		++placesDataIterator;
		++initialMarkupDataIterator;
	}
	return placesMap;
}

std::map<std::string, pn::Transition*> pn::FileInterface::getTransitions()
{
	std::vector<std::string>::iterator transitionsDataIterator = mTransitions.begin();
	std::map<std::string, pn::Transition*> transitionsMap;
	while (transitionsDataIterator != mTransitions.end())
	{
		std::string transitionId(*transitionsDataIterator);
		transitionsMap[transitionId] = new pn::Transition(transitionId, 00000);
		++transitionsDataIterator;
	}
	return transitionsMap;
}

std::map<std::string, pn::PlaceToTransition*> pn::FileInterface::getPlacesToTransitions()
{
	
}

std::map<std::string, pn::TransitionToPlace*> pn::FileInterface::getTransitionsToPlaces()
{
	
}

// ----- End of Public Methods

// ----- Begin of Protected Methods

void pn::FileInterface::readLine(const std::string& vLineToRead)
{
	int begin_position = vLineToRead.find_first_of("={");
	int end_position = vLineToRead.find_last_of("}");
	if ((begin_position != -1) && (end_position != -1))
	{
		std::string label = vLineToRead.substr(0, begin_position);
		if (label == "P")
		{
			readPlaces(vLineToRead);
			return;
		}
		if (label == "T")
		{
			readTransitions(vLineToRead);
			return;
		}
		if (label == "PxT")
		{
			readPlacesToTransition(vLineToRead);
			return;
		}
		if (label == "TxP")
		{
			readTransitionToPlaces(vLineToRead);
			return;
		}
		if (label == "M")
		{
			readInitialMarkups(vLineToRead);
			return;
		}
	}
}

void pn::FileInterface::readPlaces(std::string vLine)
{
	mnp::trim(vLine, "P={", "}");
	mPlaces = mnp::slice(vLine, ",");
}

void pn::FileInterface::readTransitions(std::string vLine)
{
	mnp::trim(vLine, "T={", "}");
	mTransitions = mnp::slice(vLine, ",");
}

void pn::FileInterface::readPlacesToTransition(std::string vLine)
{
	mnp::trim(vLine, "PxT={(", ")}");
	std::vector<std::string> fields(mnp::slice(vLine, "),("));
	std::vector<std::string>::iterator iterator;
	for (iterator = fields.begin(); iterator != fields.end(); ++iterator)
	{
		mPlacesToTransitions.push_back(mnp::slice(*iterator, ","));
	}
}

void pn::FileInterface::readTransitionToPlaces(std::string vLine)
{
	mnp::trim(vLine, "TxP={(", ")}");
	std::vector<std::string> fields(mnp::slice(vLine, "),("));
	std::vector<std::string>::iterator iterator;
	for (iterator = fields.begin(); iterator != fields.end(); ++iterator)
	{
		mTransitionsToPlaces.push_back(mnp::slice(*iterator, ","));
	}
}

void pn::FileInterface::readInitialMarkups(std::string vLine)
{
	mnp::trim(vLine, "M={", "}");
	mMarkups = mnp::slice(vLine, ",");
}

// End of Protected Methods
